package com.itzyf.pokemondata.activity.team

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.avos.avoscloud.AVObject
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.activity.illustration.CharacteristicActivity
import com.itzyf.pokemondata.activity.panel.PanelDetailActivity
import com.itzyf.pokemondata.bean.*
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2019/06/07 14:18
 */
class SelectDataActivity : BaseActivity() {
    var mDatas: MutableList<String?> = ArrayList()
    var index = 0
    private lateinit var mAdapter: QuickAdapter
    override fun getContentViewId() = R.layout.activity_list


    override fun handlerActivity() {

        mSearchView.hint = "请输入查询名称"
        mAdapter = QuickAdapter(R.layout.item_text, mDatas)
        rv_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_list.adapter = mAdapter
//        showProgressDialog()
        queryDatas()

        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })

        mAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this@SelectDataActivity, PanelDetailActivity::class.java)
            intent.putExtra("cname", mDatas[position])
            setResult(Activity.RESULT_OK, intent)
            finish()
        }

    }


    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        val table = when (intent.getIntExtra("type", -1)) {
            0 -> DbSkillDo::class.java
            1 -> DbSuperSkillDo::class.java
            2 -> DbPropDo::class.java
            3 -> DbCharacteristicDo::class.java
            else -> DbSkillDo::class.java
        }


        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where = "cname like '%$query%'"
        }
        val dataList =
            LitePal
                .select("cname")
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(table)
        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            val cname = obj::class.java.getMethod("getCname").invoke(obj)

            mDatas.add(cname as String)
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()

    }


    internal inner class QuickAdapter(layoutResId: Int, data: List<String?>) :
        BaseQuickAdapter<String, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: String?) {
            item?.let {
                helper.setText(R.id.text, item)
            }
        }
    }

    override fun getToolbarTitle() = "请选择"
}